package in.muton.edushare.pages.cp.courseFBCP;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.panels.IPollPanelContainer;
import in.muton.edushare.poll.panels.NewPoll;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.Poll;

public class PollCreationPanel extends EdusharePanel implements
		IPollPanelContainer {
	public static final int SHOW_POLL_CREATION_PANEL = 10;
	public static final int SHOW_QUESTION_ADDITION_PANEL = 20;
	public static final int SHOW_IMPORT_EXISTING_PANEL = 30;

	private int currentState = 0;
	private EdusharePanel activePanel = null;
	private CourseFeedbackPage page = null;

	public PollCreationPanel(String id, final CourseFeedbackPage page) {
		super(id, page);
		this.page = page;
		add(new Label("pollName", new Model<String>() {
			private static final long serialVersionUID = 9198006936362143186L;

			@Override
			public String getObject() {
				IModel<Poll> p = page.getActivePollModel();
				if (p != null)
					return p.getObject().getPollName();
				return "";
			}
		}));
		setActivePanel(SHOW_POLL_CREATION_PANEL);
	}

	public void setActivePanel(int panelId) {
		EdusharePanel newPanel = null;
		currentState = panelId;
		switch (panelId) {
		case SHOW_POLL_CREATION_PANEL:
			newPanel = new NewPoll("activePanel", page.getActivePollModel(),
					page, this, panelId, panelId);
			break;
		case SHOW_QUESTION_ADDITION_PANEL:
			newPanel = new PollEditPanelWrapper("activePanel", page
					.getActivePollModel(), page, this);
			break;
		case SHOW_IMPORT_EXISTING_PANEL:
			newPanel = new PollImportPanel("activePanel", page
					.getUnivModel(), this, page.getActivePollModel());
			break;
		}

		if (activePanel != null && newPanel != null) {
			activePanel.replaceWith(newPanel);
			activePanel = newPanel;
		} else if (newPanel != null) {
			activePanel = newPanel;
			add(activePanel);
		}
	}

	private static final long serialVersionUID = -2473466941031236702L;

	public void gotoNextPage() {
		if (currentState == SHOW_POLL_CREATION_PANEL
				|| currentState == SHOW_IMPORT_EXISTING_PANEL)
			setActivePanel(SHOW_QUESTION_ADDITION_PANEL);
		else if (currentState == SHOW_QUESTION_ADDITION_PANEL){
			page.setActivePanel(CourseFeedbackPage.SHOW_POLL_SETTINGS);
			page.setParent(true);
		}
	}
	
	public void setPoll(IModel<Poll> pollModel) {
		page.setActivePollModel(pollModel);
	}

	public void gotoDeletedPage() {
		page.setActivePanel(CourseFeedbackPage.SHOW_POLL_LIST);
	}
}
